package com.esun.cheetah.biz.personal.personalcommunicater;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;

import com.esun.cheetah.biz.personal.personalinfo.UserInfo;
import com.esun.cheetah.config.BizCode;
import com.esun.cheetah.config.Key;
import com.esun.cheetah.network.AbsInferiorityCommunicater;
import com.esun.util.VersionUtils;
import com.esun.util.log.EDebug;

/**
 * 
 * @Package com.esun.cheetah.biz.personal.personalcommunicater.RegistCommunicater
 * @ClassName: RegistCommunicater
 * @Description: TODO(user send register information)
 * @author handy <br/>
 * @date create at 2012-7-31 下午3:11:35
 *
 */
public class RegistCommunicater extends AbsInferiorityCommunicater {

	private String status;
	private String msg;
	private String code;
	private String mobile;
	private String ck;

	// private String imei;

	private String username = "";
	private String userpass = "";
	private Context context;

	public RegistCommunicater(Handler handler, Context context, String username, String userpass, String mobile) {
		super(handler);
		this.username = username;
		this.userpass = userpass;
		this.mobile = mobile;
		this.context = context;
		// this.imei = imei;
	}

	@Override
	public HashMap<String, String> comHashMap() {
		HashMap<String, String> hmJson = new HashMap<String, String>();
		hmJson.put(Key.KEY_ID, BizCode.BIZCODE_PERSONAL_REGISTER);
		hmJson.put(Key.KEY_USERNAME, username);
		hmJson.put(Key.KEY_USERPWD, userpass);
		hmJson.put(Key.KEY_MOBILE, mobile);
		hmJson.put(Key.PLATFROM, BizCode.ZERO);
		hmJson.put(Key.KEY_SERVIAL, VersionUtils.getInstance().getSerial(context));

		return hmJson;
	}

	@Override
	public void getJsonContent(String serverRes) throws JSONException {

		JSONObject json = new JSONObject(serverRes);
		status = json.getString(Key.STATUS);
		msg = json.getString(Key.MSG);

		EDebug.o("msg---" + msg);
		EDebug.o("status---" + status);
		if (Key.TRUE.equals(status)) {
			JSONObject jsondata = new JSONObject(json.getString(Key.DATA));
			code = jsondata.getString(Key.CODE);
			EDebug.o("code---" + code);
			ck = jsondata.getString(Key.CK);
			EDebug.o("ck---" + ck);
			saveUserInfo();
		}
	}

	@Override
	public void getServerResponseSuccess() {
		super.getServerResponseSuccess();
	}

	@Override
	public void getServerResponseException() {
		super.getServerResponseException();
	}

	private void saveUserInfo() {
		UserInfo.getUserInfo().setCk(ck);
		UserInfo.getUserInfo().setUserName(username);
		UserInfo.getUserInfo().setUserpwdMD5(userpass);
		EDebug.o("md5  ----" + UserInfo.getUserInfo().getUserpwdMD5());
	}

}
